package com.gitee.controller.user;

import com.gitee.dto.SetPasswordDTO;
import com.gitee.entity.User;
import com.gitee.result.Result;
import com.gitee.service.UserService;
import com.gitee.dto.PasswordLoginDTO;
import com.gitee.dto.PhoneLoginDTO;
import com.gitee.vo.UserVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@Slf4j
@RequestMapping("/user/userController")
@Api(tags = "用户接口")
public class UserController {

    @Resource
    private UserService userService;

    @GetMapping("/getUserByPhone/{phone}")
    @ApiOperation("根据手机号获得用户")
    public Result<User> getUserByPhone(@PathVariable String phone) {
        return Result.success(userService.getUserByPhone(phone));
    }

    @GetMapping("/getCode/{phone}")
    @ApiOperation("获得验证码")
    public Result<String> getCode(@PathVariable String phone) {
        log.info("获取验证码: {}", phone);
        return Result.success(userService.getCode(phone));
    }

    @PostMapping("/phoneLogin")
    @ApiOperation("验证码登录")
    public Result<UserVO> phoneLogin(PhoneLoginDTO phoneLoginDTO) {
        return Result.success(userService.phoneLogin(phoneLoginDTO));
    }

    @PostMapping("/passwordLogin")
    @ApiOperation("密码登录")
    public Result<UserVO> passwordLogin(PasswordLoginDTO passwordLoginDTO) {
        return Result.success(userService.passwordLogin(passwordLoginDTO));
    }

    @PutMapping("/setPassword")
    @ApiOperation("修改密码")
    public Result<?> setPassword(@RequestBody SetPasswordDTO setPasswordDTO) {
        log.info("修改密码: {}", setPasswordDTO);
        // 提供验证码修改密码
        userService.setPassword(setPasswordDTO);
        return Result.success();
    }

    @GetMapping("/loginOut")
    @ApiOperation("退出登录")
    public Result<?> loginOut() {
        log.info("退出登录");
        userService.loginOut();
        return Result.success();
    }

    @GetMapping("/getUser/{userId}")
    @ApiOperation("获得用户")
    public Result<User> getUser(@PathVariable Integer userId) {
        log.info("获得用户: {}", userId);
        return Result.success(userService.getUser(userId));
    }

    @GetMapping("/getUserByName/{name}")
    @ApiOperation("模糊查询用户")
    public Result<List<User>> getUserByName(@PathVariable String name) {
        log.info("模糊查询用户: {}", name);
        return Result.success(userService.getUserByName(name));
    }

    @PutMapping("/setUser")
    @ApiOperation("修改用户")
    public Result<?> setUser(@RequestBody User user) {
        log.info("修改用户: {}", user);
        userService.setUser(user);
        return Result.success();
    }
}
